/**
 * 表格分页参数转自定义参数
 * @param query
 * @returns
 */
export const paginationToQuery = (query): API.PageParams => {
  const { current, pageSize } = query;
  return { pageNum: current, pageSize };
};

/**
 * 隐藏重要信息 脱敏
 * @param str
 * @returns
 */
export const hideImportantInfo = (str: string): string => {
  return `${str.substring(1, 3)}****${str.substring(str.length - 3, str.length)}`;
};

/**
 * 数组分页
 * @param current 当前页
 * @param pageSize 每页条数
 * @param array 需要分页数组
 * @returns
 */
export const arrayToPagination = (current: number, pageSize: number, array) => {
  const offset = (current - 1) * pageSize;
  return offset + pageSize >= array.length
    ? array.slice(offset, array.length)
    : array.slice(offset, offset + pageSize);
};

/**
 * 过滤对象中属性的空值
 * @param obj
 * @returns
 */
export const filterEmptyValues = (obj) => {
  Object.keys(obj).filter((key) => obj[key] !== null && obj[key] !== undefined && obj[key] !== '');
  return obj;
};

/**
 * 过滤proxy类型中属性的空值
 * @param obj
 * @returns
 */
export const proxyFilterEmptyValues = (obj) => {
  const proxyValue = {};
  for (const key of Reflect.ownKeys(obj)) {
    if (obj[key] !== null && obj[key] !== undefined && obj[key] !== '') {
      proxyValue[key] = obj[key];
    }
  }
  return proxyValue;
};
